import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/views/Login'
import Home from '@/views/Home'
import Info from '@/views/Info'
import User from '@/views/User'
import Watch from '@/views/Watch'
import DeviceName from '@/views/DeviceName'
import DeviceInfo from '@/views/DeviceInfo'
import store from '@/store/store'

Vue.use(Router)

const router = new Router({
  routes: [{
      path: '/login',
      name: 'Login',
      component: Login,
      meta: {
        title: '登录'
      }
    },
    {
      path: '/',
      name: 'Home',
      component: Home,
      meta: {
        title: '登录'
      }
    },
    {
      path: '/watch',
      name: 'Watch',
      component: Watch,
      meta: {
        title: '登录'
      }
    },
    {
      path: '/devicename',
      name: 'DeviceName',
      component: DeviceName,
    },
    {
      path: '/DeviceInfo',
      name: 'DeviceInfo',
      component: DeviceInfo,
      meta: {
        title: '设备详情'
      }
    },
    {
      path: '/info',
      name: 'Info',
      component: Info,
      meta: {
        title: '登录'
      }
    },
    {
      path: '/user',
      name: 'User',
      component: User,
      meta: {
        title: '登录'
      }
    },
  ]
})

//导航守卫
// router.beforeEach(function (to, from, next) {
//   //切换页面，使滚动条置顶
//   // document.body.scrollTop = 0;
//   var openid;
//   if (store.state.openid == null) {
//     // openid = to.query.openid;
//     openid = '328563'
//     console.log(openid)
//     //拿到参数提交到store仓库 存入session中
//     // store.commit('setOpenid', openid)
//   } else {
//     openid = '328563'
//     // openid = store.state.openid
//     console.log(9 + openid)
//   }

//从url中获取openid参数，第一次进来必有参数，拿到openid验证是否注册
//    if (store.state.isLogin === true) {
//      next()
//    } else {
//验证是否注册
// axios.get(httpUrl.selectBinding, {
//   params: {
//     'openid': openid
//   }
// }).then(res => {
//   //如果注册并且没有退出登录，获取用户信息并存储在store中，便于修改用户信息。
//   if (res.data.status == true) {
//     //使用action将登录信息提交到store仓库,进行统一处理
//     store.dispatch('loginAction', res.data.data);
//     if (to.path === '/login') {
//       next('/')

//     } else {
//       next()
//     }

//   } else {
//     if (to.path === '/login') {
//       next()
//     } else {
//       next('/login')
//     }
//   }
// }).catch(error => {
//   console.log("Error:" + error)
// })
// }

// })
export default router
